/*
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package com.zavakid.scala.exercise.chapter4

import scala.reflect.internal.util.StringOps
import scala.collection.mutable.StringBuilder

/**
 * @author zavakid 2013-6-7 下午6:31:57
 * @since 1.0
 */
object Main4 extends App {

  // 1
  def no1() = {
    val prices = Map("tool1" -> 10.0, "tool2" -> 15.0, "tool3" -> 20.0)
    for ((k, v) <- prices) yield (k, v - 10)
  }

  // 2
  def no2() = {
    val in = new java.util.Scanner(new java.io.File("myfile.txt"))
    val words = new scala.collection.mutable.HashMap[String, Int]
    while (in.hasNext()) {
      val word = in.next()
      val count = words.getOrElse(word, 0)
      words += (word -> (count + 1))
    }
    words
  }

  // 3
  def no3() = {
    val in = new java.util.Scanner(new java.io.File("myfile.txt"))
    var words = Map[String, Int]()
    while (in.hasNext()) {
      val word = in.next()
      val count = words.getOrElse(word, 0)
      words += (word -> (count + 1))
    }
    words
  }

  // 4
  def no4() = {
    val in = new java.util.Scanner(new java.io.File("myfile.txt"))
    import scala.collection.JavaConversions.mapAsScalaMap
    val words: scala.collection.mutable.Map[String, Int] = new java.util.TreeMap[String, Int]
    while (in.hasNext()) {
      val word = in.next()
      val count = words.getOrElse(word, 0)
      words += (word -> (count + 1))
    }
    words
  }

  // 5
  def no5 = no4

  // 6
  def no6() = {
    import scala.collection.JavaConversions.mapAsScalaMap
    val days: scala.collection.mutable.Map[String, Int] = new java.util.LinkedHashMap[String, Int]
    import java.util.Calendar._
    days += ("Monday" -> MONDAY)
    days += ("Tuesday" -> TUESDAY)
    days += ("Wednesday" -> WEDNESDAY)
    days += ("Thursday" -> THURSDAY)
    days += ("Friday" -> FRIDAY)
    days += ("Saturday" -> SATURDAY)
    days += ("Sunday" -> SUNDAY)
    days
  }

  // 7
  def no7() {
    def getBlankStr(n: Int): String = {
      var s: StringBuilder = new StringBuilder
      for (i <- 0 until n) {
        s append " "
      }
      s toString
    }

    import scala.collection.JavaConversions.propertiesAsScalaMap
    val props: scala.collection.Map[String, String] = System.getProperties()
    val maxLength = props.keys.map(_.length()).max
    for ((k, v) <- props) {
      println(k + getBlankStr(maxLength - k.length) + " | " + v)
    }
  }
  //no7()

  // 8
  def no8(array: Array[Int]) = {
    (array.max, array.min)
  }

  // 9 
  def lteqgt(values: Array[Int], v: Int) = {
    val great = values.count(_ > v)
    val equal = values.count(_ == v)
    val less = values.count(_ < v)
    (less, equal, great)
  }

  // 10
  // 返回一个 pair 序列
  // 想把行/列进行转换的时候
  def no10(s1: String, s2: String) = {
    s1 zip s2
  }

}